<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 1/19/15
 * Time: 12:17 PM
 */

namespace DinhThi\MVC;


class View {

    protected $title;
    protected $baseURL;
    protected $template;
    protected $layout;
    protected $layoutBuffer;
    protected $headerScript;
    protected $script;
    protected $headerStyle;
    protected $style;

    private static $instance = null;
    public static function getInstance(){
        if (self::$instance == null){
            self::$instance = new View();
        }
        return self::$instance;
    }

    private function __construct(){
        $config             = Configuration::getInstance();
        $this->title        = $config->getProperty('SITENAME', 'Undefined sitename');
        $config->setProperty('BASEURL', 'http://' . $_SERVER['SERVER_NAME'] . $config->getProperty('BASEURL', ''));
        $this->baseURL      = $config->getProperty('BASEURL');
        $config->setProperty('TEMPLATE', "src/MyApp/" .$config->getProperty('MODULE', '') . '/View/' . $config->getProperty('TEMPLATE', 'Template') . '/');
        $this->template     = $config->getProperty('TEMPLATE');
        $this->layout       = $config->getProperty('LAYOUT', 'layout');
        $this->layoutBuffer = $config->getProperty('LAYOUT_BUFFER', false);
        $this->headerScript = $this->headerStyle = array();
        $this->script       = $this->style = array();
    }

    public function setLayoutBuffer($buffer = true){
        $this->layoutBuffer = $buffer;
    }

    public function addHeaderScript($filename){
        $this->headerScript[] = $filename;
    }

    public function addHeaderStyle($filename){
        $this->headerStyle[] = $filename;
    }

    public function render($name, $single = false){
        if (!$this->layoutBuffer){
            if (!$single){
                foreach ($this->headerScript as $value){
                    $this->script[] = '<script src = "' . $this->getTemplateURL() . $value . '"></script>';
                }
                foreach ($this->headerStyle as $value){
                    $this->style[] = '<link rel="stylesheet" type="text/css" href = "' . $this->getTemplateURL() . $value . '" />';
                }
                include($this->template . 'Layout/Include/header.php');
                include($this->template . $name . '.php');
                include($this->template . 'Layout/Include/footer.php');
            } else{
                include($this->template . $name . '.php');
            }
        } else{
            if (!$single){
                foreach ($this->headerScript as $value){
                    $this->script[] = '<script src = "' . $this->getTemplateURL() . $value . '"></script>';
                }
                foreach ($this->headerStyle as $value){
                    $this->style[] = '<link rel="stylesheet" type="text/css" href = "' . $this->getTemplateURL() . $value . '" />';
                }

                /**
                 * Read the content
                 */
                ob_start();
                include($this->template . $name . '.php');
                $this->content = ob_get_contents();
                ob_end_clean();
                include($this->template . 'Layout/' . $this->layout . '.php');
            } else{
                include($this->template . $name . '.php');
            }
        }
    }

    public function setTitle($title, $replace = false){
        if ($replace){
            $this->title = $title;
        } else{
            $this->title = $this->title . ' - ' . $title;
        }
    }

    public function setLayout($layoutName){
        $this->layout = $layoutName;
    }

    public function getBaseURL(){
        return $this->baseURL;
    }

    public function getTemplateURL(){
        return $this->baseURL . $this->template;
    }
} 